package cn.lxh.controller.remote;

import cn.hutool.core.bean.BeanUtil;
import cn.lxh.pojo.domain.DriverSummary;
import cn.lxh.remote.api.DriverFeignApi;
import cn.lxh.remote.pojo.result.DriverSummaryResult;
import cn.lxh.result.JSONResult;
import cn.lxh.service.IDriverSummaryService;
import cn.lxh.utils.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * DriverFeignApiImpl控制层
 */
@RestController
@Slf4j
public class DriverFeignApiImpl implements DriverFeignApi {

    @Autowired
    private IDriverSummaryService driverSummaryService;

    /**
     * 获取司机今日取消订单和今日投诉订单
     * @param driverId
     * @return
     */
    @Override
    public JSONResult<DriverSummaryResult> getDriverSummary(Long driverId) {
        // 根据司机id获取司机汇总
        DriverSummary driverSummary = driverSummaryService.getById(driverId);
        AssertUtil.isNotNull(driverSummary, "获取司机汇总失败");
        // 属性拷贝
        DriverSummaryResult driverSummaryResult = BeanUtil.copyProperties(driverSummary, DriverSummaryResult.class);
        return JSONResult.success(driverSummaryResult);
    }

    @Override
    public JSONResult addTodayComplaintOrder(Long driverId) {
        // 根据司机id获取司机汇总
        DriverSummary driverSummary = driverSummaryService.getById(driverId);
        AssertUtil.isNotNull(driverSummary, "获取司机汇总失败");
        // 设置司机今日投诉订单加1
        driverSummary.setTodayComplaint(driverSummary.getTodayComplaint() + 1);
        driverSummary.setUpdateTime(new Date());
        driverSummaryService.updateById(driverSummary);
        // 修改司机汇总
        return JSONResult.success();
    }

    @Override
    public JSONResult subTodayComplaintOrder(Long driverId) {
        // 根据司机id获取司机汇总
        DriverSummary driverSummary = driverSummaryService.getById(driverId);
        AssertUtil.isNotNull(driverSummary, "获取司机汇总失败");
        // 设置司机今日投诉订单减1
        driverSummary.setTodayComplaint(driverSummary.getTodayComplaint() - 1);
        driverSummary.setUpdateTime(new Date());
        driverSummaryService.updateById(driverSummary);
        // 修改司机汇总
        return JSONResult.success();
    }


}
